package com.byedu.course;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import com.byedu.domain.course.LibraryDO;
import com.byedu.domain.course.vo.LibraryVO;
import com.byedu.domain.course.vo.enumvos.IndustryEnumVO;
import com.byedu.domain.course.vo.enumvos.LibraryTypeEnumVO;
import com.byedu.domain.focus.BResultDO;
import com.byedu.model.enums.IndustryEnum;
import com.byedu.model.enums.LibraryTypeEnum;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byedu.mapper.course.LibraryMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by chenxigui on 2017/4/26.
 */
@Service
public class LibraryService {
	
    @Autowired
    private LibraryMapper libraryMapper;

    /**
     * @description 获取专题列表
     * @author chenxigui
     * @date 2017-04-26
     * @param courseId
     * @return
     */
    public List<LibraryVO> listLibrariesByCourseId(Integer courseId) {
        return convertDoListToVoList(libraryMapper.listLibrariesByCourseId(courseId));
    }

    public LibraryDO getLibraryDOById(Integer libraryId) {
        return libraryMapper.getLibraryDOById(libraryId);
    }

    public List<LibraryVO> listAllLibraries() {
        return convertDoListToVoList(libraryMapper.listAllLibraries());
    }

    public List<LibraryVO> convertDoListToVoList(List<LibraryDO> libraryDOList) {
        List<LibraryVO> libraryVOList = new ArrayList<LibraryVO>();
        if (CollectionUtils.isNotEmpty(libraryDOList)) {

            for (LibraryDO libraryDO : libraryDOList) {


                LibraryVO libraryVO = new LibraryVO();
                try {
                    BeanUtils.copyProperties(libraryVO, libraryDO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                libraryVOList.add(libraryVO);
            }

            return libraryVOList;
        }

        return libraryVOList;
    }


    @Transactional
    public int saveLibrarytest( LibraryVO libraryVO) throws InvocationTargetException, IllegalAccessException {
        LibraryDO libraryDO = new LibraryDO();

        BeanUtils.copyProperties(libraryDO, libraryVO);
        int retnum=0;
        for(int i=0 ;i<5;i++) {
            retnum = libraryMapper.saveLibraryDO(libraryDO);
            System.out.println("libraryDO.getId()---" + libraryDO.getId());

            System.out.println("retnum---" + retnum);

            if (i>3) {
                throw new RuntimeSqlException("手动模拟转账时出现异常");
            }
        }
        return retnum;
    }

//    @Transactional
//    public int saveLibrary( LibraryVO libraryVO) throws InvocationTargetException, IllegalAccessException {
//        LibraryDO libraryDO = new LibraryDO();
//
//            BeanUtils.copyProperties(libraryDO, libraryVO);
//        int retnum=0;
//        for(int i=0 ;i<5;i++) {
//             retnum = libraryMapper.saveLibraryDO(libraryDO);
//            System.out.println("libraryDO.getId()---" + libraryDO.getId());
//
//            System.out.println("retnum---" + retnum);
//
//            if (i>3) {
//                throw new RuntimeSqlException("手动模拟转账时出现异常");
//            }
//        }
//        return retnum;
//    }

    public int saveLibrary( LibraryDO libraryDO) {

        return libraryMapper.saveLibraryDO(libraryDO);
    }

    public int saveLibraryString(String title) {

        return 0;
    }

    public List<IndustryEnumVO> listIndustryEnum() {
        List<IndustryEnumVO> list = new ArrayList<IndustryEnumVO>();

        for (IndustryEnum industryEnum : IndustryEnum.values()) {
            IndustryEnumVO industryEnumVO = new IndustryEnumVO();
            industryEnumVO.setName(industryEnum.name());
            industryEnumVO.setValue(industryEnum.getTitle());
            list.add(industryEnumVO);
        }

        return list;
    }

    public List<LibraryTypeEnumVO> listLibraryEnum() {

        List<LibraryTypeEnumVO> list = new ArrayList<LibraryTypeEnumVO>();

        for (LibraryTypeEnum libraryTypeEnum : LibraryTypeEnum.values()) {
            LibraryTypeEnumVO libraryTypeEnumVO = new LibraryTypeEnumVO();
            libraryTypeEnumVO.setName(libraryTypeEnum.name());
            libraryTypeEnumVO.setValue(libraryTypeEnum.getTitle());
            list.add(libraryTypeEnumVO);
        }

        return list;
    }

    public int del(int pid) {
        return libraryMapper.del(pid);
    }

    public List<BResultDO> getCourseCountById() {
        return  libraryMapper.getCourseCountById();
    }

    public List<BResultDO> getCourseCountByUserId() {
        return  libraryMapper.getCourseCountByUserId();
    }

}
